package ibase.solr.formatter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SearchPagination {

	private int pageSize;
	private int index;
	private List alist;
	private List pageList;
	
	public SearchPagination(Collection collection , int pageSize)
	{
		this.pageSize = pageSize;
	    this.index = 0;
	    this.alist = new ArrayList(collection);
	    //recursivePaginate();
	}
	
	public List recursivePaginate()
	{
		int start = index * pageSize;
		int end = start + pageSize-1;
		if (end >= alist.size()) 
	    {
	       end = alist.size() - 1;
	    }
		if (start >= alist.size()) 
	    {
	       index = 0;
	       recursivePaginate();
	    }
		else if (start < 0) 
	    {
	       index = alist.size() / pageSize;
	       if (alist.size() % pageSize == 0) 
	       {
	         index--;
	       }
	       recursivePaginate();
	    }
		else 
	    {
	       pageList = alist.subList(start, end + 1);
	    }
		return pageList;
	}
	
	public void gotoPage(int pageNumber) 
	{
	    index = pageNumber;
	    recursivePaginate();
	}
}
